package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Employee;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentServicePlus;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apiguardian.api.API;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * @author hkj
 * @email 1349091387@qq.com
 * @date 2021/11/25
 * 源码允许修改，并且本人十分乐意有 大佬 来对代码进行 随 意 调 教
 **/
@Service
public class AppointmentServiceImplPlus extends ServiceImpl<AppointmentMapper, Appointment> implements IAppointmentServicePlus {

    @Autowired
    private AppointmentMapper appointmentMapper;

    @Override
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
        IPage<Appointment> page = new Page<>(qo.getPageNum(),qo.getPageSize());
        QueryWrapper<Appointment> wrapper = new QueryWrapper<>();
        wrapper.isNull("isDel").like(StringUtils.hasText(qo.getName()),"customer_name",qo.getName())
                .like(StringUtils.hasText(qo.getCustomerPhone()),"customer_phone",qo.getCustomerPhone())
                .eq(qo.getStatus() != null,"status",qo.getStatus());
        return new TablePageInfo<Appointment>(super.page(page,wrapper));
    }

    @Override
    public void softRemove(Long id) {
        //只有是预约状态时才允许删除
        /*Appointment appointment = this.get(id);
        if(!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())){
            throw new BusinessException("只有是预约状态才可以进行删除操作");
        }
        appointmentMapper.softRemove(id);*/
        Appointment appointment = appointmentMapper.selectById(id);
        if(!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())){
            throw new BusinessException("只有是预约状态才可以进行删除操作");
        }
        UpdateWrapper<Appointment> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        wrapper.set("isDel",1);
        appointmentMapper.update(appointment,wrapper);
    }

    @Override
    public void cancel(Long id) {
        //只有是预约状态时才允许取消
        Appointment appointment = appointmentMapper.selectById(id);
        if(!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())){
            throw new BusinessException("只有是预约状态才可以进行取消操作");
        }
        //appointmentMapper.changeStatus(id,Appointment.STATUS_CANCEL);
        UpdateWrapper<Appointment> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        wrapper.set("status",Appointment.STATUS_CANCEL);
        appointmentMapper.update(appointment,wrapper);
    }

    @Override
    public void arrival(Long id) {
        /*Appointment appointment = this.get(id);
        if(!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())){
            throw new BusinessException("只有是预约状态中才可以修改为到店状态");
        }
        //通过id将状态修改为已到店，并且还要将到店时间修改为当前时间
        // 可以给当前方法加一个时间参数，也可以在sql中使用函数来表示当前时间
        appointmentMapper.arrival(id,Appointment.STATUS_ARRIVAL);*/
        Appointment appointment = appointmentMapper.selectById(id);
        if(!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())){
            throw new BusinessException("只有是预约状态中才可以修改为到店状态");
        }
        UpdateWrapper<Appointment> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        wrapper.set("actual_arrival_time",new Date());
        wrapper.set("status",Appointment.STATUS_ARRIVAL);
        appointmentMapper.update(appointment,wrapper);
    }
}
